package com.two.screen.water.balance.print.all

import android.annotation.SuppressLint
import com.two.screen.water.balance.net.api.DataApi
import com.two.screen.water.balance.print.bean.PrintConnectBean
import com.two.screen.water.balance.print.bean.PrintConnectSaleReportBean
import com.two.screen.water.balance.print.bean.PrintOrderBean
import com.two.screen.water.balance.print.bean.PrintServerCollectOrderBean
import com.two.screen.water.balance.ui.App
import com.two.screen.water.balance.utils.CommUtils
import com.two.screen.water.balance.utils.isNotAvailable

object USBPrinterAll {

    var mIsOpenSuccess = false
    @SuppressLint("StaticFieldLeak") var usbPrinter: UsbPrinter? = null//USB打印

    fun initUsbDevice() {
        usbPrinter = UsbPrinter.getInstance()
        usbPrinter?.initPrinter(App.context)
    }

    fun d(msg: String) {
        CommUtils.println("USBPrinterAutoReply->", msg)
    }

    fun closePrinter() {
        try {
            usbPrinter?.close()
        } catch (e: Exception) {
            d("关闭打印失败：$e")
        }
    }

    fun printTest() {
        usbPrinter?.init()
        var times = DataApi.mBillPrintTimesArray[DataApi.mBillPrintTimesIndex].toInt()

        for (time in 1..times) {
            usbPrinter?.setAlign(1)
            usbPrinter?.bold(true)
            usbPrinter?.printTextNewLine("测试打印")
            usbPrinter?.bold(false)
            usbPrinter?.setAlign(0)
            usbPrinter?.printTextNewLine("一二三四五六七八九十")
            usbPrinter?.printTextNewLine("==============================================")
            usbPrinter?.printLine(3)
            usbPrinter?.setAlign(1)
            usbPrinter?.printQrCode("测试打印二维码")
            usbPrinter?.init()
            usbPrinter?.setAlign(0)
            usbPrinter?.printLine(3)
            usbPrinter?.printTextNewLine("12345678910..")
            usbPrinter?.printTextNewLine("")
            usbPrinter?.printTextNewLine("")
            usbPrinter?.printTextNewLine("")
            usbPrinter?.printLine(2)
            d("printTest finish times:${time + 1}")
        }
        //usbPrinter?.cutPager()
    }

    fun printOrder(data: PrintOrderBean?) {
        usbPrinter?.init()
        var times = DataApi.mBillPrintTimesArray[DataApi.mBillPrintTimesIndex].toInt()
        for (time in 1..times) {
            usbPrinter?.setAlign(1)
            usbPrinter?.bold(true)
            usbPrinter?.setTextSize(3)
            usbPrinter?.printTextNewLine(data?.title)
            usbPrinter?.setTextSize(0)
            usbPrinter?.bold(false)
            usbPrinter?.printLine(2)//居中需要换行
            usbPrinter?.setAlign(0)

            usbPrinter?.printTextNewLine("市场名称：${DataApi.mMarketName.isNotAvailable()}")
            usbPrinter?.printTextNewLine("商户名称：${DataApi.mShopName.isNotAvailable()}")
            usbPrinter?.printTextNewLine("档口号：${DataApi.mShopStallName.isNotAvailable()}")
            usbPrinter?.printTextNewLine("订单编号：${data?.orderSn.isNotAvailable()}")
            usbPrinter?.printTextNewLine("收银时间：${data?.collectionTime.isNotAvailable()}")
            usbPrinter?.printTextNewLine("操作人：${data?.operationName.isNotAvailable()}")//原本是操作人员，2023年9月14日 17:02:53 渣渣昆叫改成商户名称
            usbPrinter?.printTextNewLine(data?.orderPriceInfo.isNotAvailable())
            usbPrinter?.printTextNewLine("支付信息：${data?.payInfo.isNotAvailable()}")

            if (data?.productList != null && data.productList!!.isNotEmpty()) {
                usbPrinter?.printTextNewLine("-------------------------------")
                usbPrinter?.printTextNewLine("品名")
                val tableHead = "数量;单价;金额"
                var table: Table? = Table(tableHead, ";", intArrayOf(14, 10, 14))
                table?.addRow("-------------------------------")
                data.productList?.indices?.forEach {
                    var bean = data.productList!![it]
                    table?.addRow(bean.name ?: "商品")
                    table?.addRow("${bean.weight ?: "0.000"};${bean.price ?: "0.00"};${bean.subPrice ?: "0.00"}")
                }
                usbPrinter?.printText(table?.tableText)
                usbPrinter?.printTextNewLine("-------------------------------")
            }
            usbPrinter?.setAlign(0)
            usbPrinter?.printTextNewLine(data?.orderTotal.isNotAvailable())
            usbPrinter?.printLine()
            usbPrinter?.setAlign(1)
            usbPrinter?.bold(true)
            if (data?.qrCode != null && data.qrCode!!.isNotBlank()) {
                usbPrinter?.printTextNewLine(data.qrCodeTipsTop.isNotAvailable())
                //usbPrinter?.bold(false)
                usbPrinter?.printLine(2)
                usbPrinter?.printQrCode(data.qrCode)
                usbPrinter?.printLine(2)

            }
//        usbPrinter?.setAlign(1)
//        usbPrinter?.bold(true)
            usbPrinter?.printText(data?.qrCodeTipsBottom.isNotAvailable())
            usbPrinter?.init()
            usbPrinter?.bold(false)
            usbPrinter?.setAlign(0)
            usbPrinter?.printLine(7)
            //usbPrinter?.cutPager()
        }
    }

    fun printConnectStatics(data: PrintConnectBean?) {
        usbPrinter?.init()
        var times = DataApi.mBillPrintTimesArray[DataApi.mBillPrintTimesIndex].toInt()
        for (time in 1..times) {
            usbPrinter?.setAlign(1)
            usbPrinter?.bold(true)
            usbPrinter?.setTextSize(3)
            usbPrinter?.printTextNewLine(data?.title)
            usbPrinter?.setTextSize(0)
            usbPrinter?.bold(false)
            usbPrinter?.printLine(2)//居中需要换行
            usbPrinter?.setAlign(0)
            usbPrinter?.printTextNewLine("收银员：${data?.operationName.isNotAvailable()}")
            usbPrinter?.printTextNewLine("起始时间：${data?.startTime.isNotAvailable()}")
            usbPrinter?.printTextNewLine("结束时间：${data?.endTime.isNotAvailable()}")
            usbPrinter?.printTextNewLine("-------------------------------")
            usbPrinter?.printTextNewLine("总单数：　　　　${data?.billTotal.isNotAvailable()}")
            usbPrinter?.printTextNewLine("门店：　　　　　${data?.billShop.isNotAvailable()}")
            usbPrinter?.printTextNewLine("退货：　　　　　${data?.billReturn.isNotAvailable()}")
            usbPrinter?.printTextNewLine("反结账：　　　　${data?.billReverse.isNotAvailable()}")
            usbPrinter?.printTextNewLine("-------------------------------")
            usbPrinter?.printTextNewLine("总实收金额：　　${data?.realTotalPrice.isNotAvailable()}")
            usbPrinter?.printTextNewLine("现金实收：　　　${data?.realCashPrice.isNotAvailable()}")
            usbPrinter?.printTextNewLine("微会员实收：　　${data?.realWetchVipPrice.isNotAvailable()}")
            usbPrinter?.printTextNewLine("聚合支付实收：　${data?.realJuHePrice.isNotAvailable()}")
            usbPrinter?.printTextNewLine("扫码收款实收：　${data?.realScanQrPrice.isNotAvailable()}")
            usbPrinter?.printTextNewLine("-------------------------------")
            usbPrinter?.printTextNewLine("总销售额：　　　${data?.saleTotalPrice.isNotAvailable()}")
            usbPrinter?.printTextNewLine("现金：　　　　　${data?.saleCashPrice.isNotAvailable()}")
            usbPrinter?.printTextNewLine("微会员：　　　　${data?.saleWetchVipPrice.isNotAvailable()}")
            usbPrinter?.printTextNewLine("聚合支付：　　　${data?.saleJuHePrice.isNotAvailable()}")
            usbPrinter?.printTextNewLine("扫码收款：　　　${data?.saleScanQrPrice.isNotAvailable()}")
            usbPrinter?.printTextNewLine("-------------------------------")
            usbPrinter?.printLine(7)
            //usbPrinter?.cutPager()
        }
    }

    fun printConnectSaleReport(data: PrintConnectSaleReportBean?) {
        usbPrinter?.init()
        var times = DataApi.mBillPrintTimesArray[DataApi.mBillPrintTimesIndex].toInt()
        for (time in 1..times) {
            usbPrinter?.setAlign(1)
            usbPrinter?.bold(true)
            usbPrinter?.setTextSize(3)
            usbPrinter?.printTextNewLine(data?.title)
            usbPrinter?.setTextSize(0)
            usbPrinter?.bold(false)
            usbPrinter?.printLine(2)//居中需要换行
            usbPrinter?.setAlign(0)
            usbPrinter?.printTextNewLine("收银员：${data?.operationName.isNotAvailable()}")
            usbPrinter?.printTextNewLine("统计时间：${CommUtils.toYMD_HMS(System.currentTimeMillis() / 1000)}")
            usbPrinter?.printTextNewLine("起始 ${data?.startTime.isNotAvailable()}")
            usbPrinter?.printTextNewLine("结束 ${data?.endTime.isNotAvailable()}")

            if (data?.productList != null && data.productList!!.isNotEmpty()) {
                usbPrinter?.printTextNewLine("-------------------------------")
                usbPrinter?.printTextNewLine("品名")
                val tableHead = "数量;单价;小计"
                var table: Table? = Table(tableHead, ";", intArrayOf(14, 10, 14))
                table?.addRow("-------------------------------")
                data.productList?.indices?.forEach {
                    var bean = data.productList!![it]
                    table?.addRow(bean.name ?: "商品")
                    table?.addRow("${bean.weight ?: "0.000"};${bean.price ?: "0.00"};${bean.subPrice ?: "0.00"}")
                }
                usbPrinter?.printTextNewLine(table?.tableText)
                usbPrinter?.printTextNewLine("-------------------------------")
            }
            usbPrinter?.printTextNewLine(data?.total.isNotAvailable())
            usbPrinter?.printLine(7)
            //usbPrinter?.cutPager()
        }
    }

    fun printServerCollectOrder(printBean: PrintServerCollectOrderBean) {
        usbPrinter?.init()
        var times = DataApi.mBillPrintTimesArray[DataApi.mBillPrintTimesIndex].toInt()
        for (time in 1..times) {
            usbPrinter?.setAlign(1)
            usbPrinter?.bold(true)
            usbPrinter?.setTextSize(3)
            usbPrinter?.printTextNewLine(printBean.title)
            usbPrinter?.setTextSize(0)
            usbPrinter?.bold(false)
            usbPrinter?.printLine(2)//居中需要换行
            usbPrinter?.setAlign(0)
            usbPrinter?.printTextNewLine("${printBean.shopName}")
            usbPrinter?.printTextNewLine("${printBean.startTime}")
            usbPrinter?.printTextNewLine("${printBean.endTime}")
            usbPrinter?.printTextNewLine("-------------------------------")

            usbPrinter?.printTextNewLine("${printBean.totalPrice}")
            usbPrinter?.printTextNewLine("${printBean.findZeroPrice}")
            usbPrinter?.printTextNewLine("${printBean.giftPrice}")
            usbPrinter?.printTextNewLine("${printBean.realPrice}")
            usbPrinter?.printTextNewLine("${printBean.orderNumbers}")
            usbPrinter?.printTextNewLine("-------------------------------")

            usbPrinter?.printTextNewLine("${printBean.cashPrice}")
            usbPrinter?.printTextNewLine("${printBean.miniProgramPrice}")
            usbPrinter?.printTextNewLine("${printBean.vipPirce}")
            usbPrinter?.printTextNewLine("${printBean.juhePrice}")
            usbPrinter?.printTextNewLine("${printBean.scanQrPrice}")

            usbPrinter?.printLine(7)
        }
    }
}